package com.gmrz.uas.plugin.authservice;

import com.gmrz.uas.plugin.authservice.exception.InvalidConfigException;
import com.gmrz.uas.plugin.exception.PluginException;
import com.gmrz.uas.plugin.provider.ServiceProvider;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

/**
 * Created by gongyuqiang on 2017/5/24.
 */
public class BaseAuthServicePlugin implements AuthServicePlugin{

    protected Map<String, String> allConfig = null;
    protected Map<String, AuthServiceConfigSpec> allConfigSpec = null;
    protected Map<String, AuthServiceDataSpec>  allDataSpec = null;
    protected ServiceProvider uasServiceProvider = null;


    public void close(Connection c)
    {
        try
        {
            if (c != null)
                c.close();
        }
        catch (Exception ex)
        {

        }
    }

    public void close(Statement s)
    {
        try
        {
            if (s != null)
                s.close();
        }
        catch (Exception ex)
        {

        }
    }

    public void close(ResultSet rs)
    {
        try
        {
            if (rs != null)
                rs.close();
        }
        catch (Exception ex)
        {
        }
    }
    @Override
    public String getPluginId() {
        return null;
    }

    @Override
    public void setServiceProvider(ServiceProvider provider) {
        this.uasServiceProvider = provider;
    }

    @Override
    public Map<String, AuthServiceConfigSpec> getAllConfigSpec() {
        return this.allConfigSpec;
    }

    @Override
    public Map<String, AuthServiceDataSpec> getAllDataSpec() {
        return this.allDataSpec;
    }

    @Override
    public Map<String, String> getAllConfig() {
        return this.allConfig;
    }

    @Override
    public void setAllConfig(Map<String, String> allConfig) throws InvalidConfigException {
        if(allConfigSpec != null){
            //检测丢失的参数
            for(String key : allConfigSpec.keySet()){
                if(!allConfigSpec.get(key).isOptional() && !allConfig.containsKey(key)){
                    throw new InvalidConfigException("Key[" + key + "] is missing");
                }
            }
        }

        this.allConfig = allConfig;
    }

    @Override
    public List<AuthServiceData> pregregister(String userId) throws PluginException {
        return null;
    }

    @Override
    public void register(String userId, List<AuthServiceData> contents) throws PluginException {

    }

    @Override
    public void unregister(String userId) throws PluginException {

    }

    @Override
    public List<AuthServiceData> preauthenticate(String userId) throws PluginException {
        return null;
    }

    @Override
    public void authenticate(String userId, List<AuthServiceData> contents) throws PluginException {
    }
}
